package com.web.gobang.view;

import com.web.gobang.connect.MyIPTool;
import com.web.gobang.connect.MySocket;
import com.web.gobang.game.GameOnline;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import static com.web.gobang.connect.MySocket.names;
import static com.web.gobang.data.PlayerList.playerList;
import static com.web.gobang.view.ChooseView.myPlayer;
import static com.web.gobang.view.MainFrame.mainFrame;

public class OnlineRoom extends JFrame {
    private JButton btnCreateRoom, btnAddRoom;
    // 窗体大小
    private int width = 500;
    private int height = 400;

    public static JPanel container;
    public static OnlineRoom onlineRoom;

    public OnlineRoom(){
        //初始化所有在线玩家
        allOnlinePlayers();

        onlineRoom = this;
        //设置窗口位置在窗口中心
        int sWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
        int sHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
        this.setLocation((sWidth - width) / 2, (sHeight - height) / 2);
//        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        onlineRoom.setTitle("联机五子棋");
        this.setSize(width, height);
        this.setResizable(false);

        //父容器
        container = new JPanel();
        container.setLayout(null);
        this.setContentPane(container);

        //在线玩家panel
        JPanel onlinePlayers = new JPanel();
        container.add(onlinePlayers);
        onlinePlayers.setBounds(0,10,130,600);
        TitledBorder titledBorder = BorderFactory.createTitledBorder("在线玩家");
        titledBorder.setTitleJustification(TitledBorder.CENTER);
        onlinePlayers.setBorder(titledBorder);
        //玩家List
//        String[] names = {"张三：192.168.19.1","李四：192.168.130.65","王五：170.10.207.1","小明：100.29.10.1","小红：192.168.130.28"};
        JList<String> PlayerList = new JList<>(names);
        onlinePlayers.add(PlayerList);

        //创建房间和加入房间按钮
        btnCreateRoom = new JButton("创建房间");
        btnAddRoom = new JButton("加入房间");
        btnCreateRoom.setBounds(170,20,110,30);
        btnAddRoom.setBounds(320,20,110,30);
        container.add(btnCreateRoom);
        container.add(btnAddRoom);

        //房间体
        JPanel room = new JPanel();
        room.setLayout(null);
        room.setBorder(BorderFactory.createTitledBorder("房间"));
        room.setBounds(150,70,300,250);
        JLabel usernameLab = new JLabel("房主：");
        usernameLab.setBounds(10,20,50,20);
        JLabel username = new JLabel(myPlayer.getName());
        username.setBounds(50,20,50,20);
        JLabel addressLab = new JLabel("IP地址：");
        addressLab.setBounds(10,60,50,20);
        List<String> res = MyIPTool.getAllLocalHostIP();
        String localIP = "<html><body>";
        String allIp = res.stream().collect(Collectors.joining("<br>"));
        localIP = localIP + "<br>" + allIp + "</html></body>";
        JLabel address = new JLabel(localIP);
        address.setBounds(60,50,200,200);
        room.add(usernameLab);
        room.add(username);
        room.add(addressLab);
        room.add(address);
        container.add(room);
        addListener();
    }

    public static void init(){
        onlineRoom.repaint();
    }

    public void addListener(){
        btnCreateRoom.addActionListener(event -> {

            try {
                MySocket.server.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            MainFrame mainFrame = new MainFrame();
            mainFrame.setVisible(true);
            MainFrame.init();
            GameOnline.reStart();
            MyDialog.createRoom();
        });
        btnAddRoom.addActionListener(event -> {

            try {
                MySocket.socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            MainFrame mainFrame = new MainFrame();
            mainFrame.setVisible(true);
            MainFrame.init();
            GameOnline.reStart();
            MyDialog.joinRoom();
        });

    }

    //查询所有在线用户
    public void allOnlinePlayers(){

    }

    // 窗口关闭事件
    @Override
    protected void processWindowEvent(WindowEvent e) {
        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
            close();
        } else {
            super.processWindowEvent(e); // 该语句会执行窗口事件的默认动作(如：隐藏)
        }
    }
    public static void close() {
        int i = JOptionPane.showConfirmDialog(null, "确定要退出游戏吗？", "正在退出五子棋...",
                JOptionPane.YES_NO_OPTION);
        if (i == JOptionPane.YES_OPTION) {
//            System.exit(0);
            onlineRoom.dispose();
//            mainFrame.dispose();
            MySocket.close();
        }
    }

}
